<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            扫码方式验证
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 350px;">1、扫描(keypress)</div>
                    <input type="text" class="aui-input" placeholder="请扫描条码" id="tm" onclick="getIdSelector();">
                    <!-- <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem"
                        onclick="toScanner();"></i> -->
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 350px;">2、扫描(scan)</div>
                    <input type="text" class="aui-input" placeholder="请扫描条码" id="tm2">
                    <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem"
                        onclick="toScanner();"></i>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 350px;">3、扫描(input)</div>
                    <input type="text" class="aui-input" placeholder="请扫描条码" id="tm3">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 350px;">4、扫描(change)</div>
                    <input type="text" class="aui-input" placeholder="请扫描条码" id="tm4">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 350px;">5、扫描(keyup)</div>
                    <input type="text" class="aui-input" placeholder="请扫描条码" id="tm5">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 350px;">6、扫描(getElementById)</div>
                    <input type="text" class="aui-input" placeholder="请扫描条码" id="tm6">
                </div>
            </li>
        </ul>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    var scanRule = ""; //条码解析规则
    var timer;
    //初始化查询仓库下拉框
    apiready = function () {
        $("#tm").focus();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        $("#" + el.id).select();
    }

    //扫描条码
    function toScanner() {
        console.log(2)
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            tmSplit2(ret);

        });
    }

    //条码监听回车事件
    $("#tm").on('keypress', function (e) {
        console.log(1)
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            //条码解析
            tmSplit(tm);
        }
    });

    $("#tm5").on('keyup', function (e) {
        if (e.key === 'Enter' || e.keyCode === 13) {
            tmSplit5($(this).val());
        }
    });

    $("#tm3").on('input', function () {
        const tm = $(this).val();
        if (tm.trim() !== "") {  // 确保值非空
            tmSplit3(tm);         // 调用条码解析函数
        }
    });

    $("#tm4").on('change', function () {
        const tm = $(this).val();
        if (tm.trim() !== "") {
            tmSplit4(tm);
        }
    });

    //扫描之后条码解析
    function tmSplit(sm) {
        console.log(11)
        $("#tm").val(sm);
    }

    //扫描之后条码解析
    function tmSplit2(sm) {
        console.log(22)
        $("#tm2").val(sm);
    }

    //扫描之后条码解析
    function tmSplit3(sm) {
        console.log(33)
        $("#tm3").val(sm);
    }

    //扫描之后条码解析
    function tmSplit4(sm) {
        console.log(44)
        $("#tm4").blur()
        $("#tm4").val(sm);
    }

    //扫描之后条码解析
    function tmSplit5(sm) {
        console.log(55)
        $("#tm5").val(sm);
    }

    document.getElementById("tm6").addEventListener("input", function () {
        tmSplit6(this.value);
    });

    function tmSplit6(sm) {
        console.log(6)
        $("#tm6").val(sm);
    }






    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>